﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ChronoScript_Compiler.Language.Parser
{
    public partial class Parser
    {
        public LanguageDefinition.Method ParseMethod()
        {
            if (PeekToken() is Tokens.Statement && PeekToken(1) is Tokens.Statement)
            {
                LanguageDefinition.Method method = new LanguageDefinition.Method();
                method.type = ReadToken().ToString();
                method.name = ReadToken().ToString();
                if (!(PeekToken() is Tokens.openParenthesis))
                {
                    ReadToken();
                    Console.WriteLine("( Excepted at " + ReadToken().lineNumber);
                }
                ReadToken();
                while (!(PeekToken() is Tokens.closeParenthesis))
                {
                    method.arguments.Add(ParseDeclaration());
                    if (PeekToken() is Tokens.Comma)
                    {
                        ReadToken();
                        if (PeekToken() is Tokens.closeParenthesis)
                            Console.WriteLine("Unexpected ,");
                    }
                }
                ReadToken();
                method.body = ParseBlock();
                return method;
            }
            else
            {
                Console.WriteLine("Method Excepted");
                throw new Exception("Method Excepted");
            }
        }
    }
}
